package com.bazhua.bazo.controller;

import com.bazhua.bazo.repository.entity.BannerInfo;
import com.bazhua.bazo.repository.model.BannerSearch;
import com.bazhua.bazo.repository.model.result.BaseSearchModel;
import com.bazhua.bazo.repository.model.result.CommonResult;
import com.bazhua.bazo.service.IBannerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Api(tags = "广告接口")
@RestController
@RequestMapping("/api/banner")
public class BannerController {

    @Autowired
    private IBannerService bannerService;

    @ApiOperation("获取全部广告轮播图")
    @RequestMapping(value = "/getList", method = RequestMethod.GET)
    @ResponseBody
    private CommonResult GetBannerList() {
        return bannerService.GetBannerList();
    }

    @ApiOperation("分页获取广告轮播图--后台")
    @RequestMapping(value = "/getPage", method = RequestMethod.POST)
    @ResponseBody
    private CommonResult GetBannerPage(@RequestBody BannerSearch search) {
        return bannerService.GetBannerPage(search);
    }

    @ApiOperation("上传广告图片--后台")
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult Upload(@RequestPart MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename();
        InputStream stream = file.getInputStream();
        return bannerService.UploadBanner(fileName, stream);
    }

    @ApiOperation("保存广告信息--后台")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult save(@RequestBody BannerInfo bannerInfo) {
        return bannerService.saveBanner(bannerInfo);
    }

    @ApiOperation("批量删除--后台")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult Delete(@RequestBody List<Long> bannerIds) {
        return bannerService.DeleteBanner(bannerIds);
    }
}
